'use strict';

angular.module('snPaas.control',[]).service('SpHttp',['$http','SpSlider',function($http,SpSlider){
	var _sucHeap = {},
		_errHeap  = {},
		_nowId  = false;
	function busy() {
      document.body.style.cssText = "cursor: progress !important";
    }
    function idle() {
      document.body.style.cssText = "";
    }

	var sendReq = function(url,params,method){
		busy();
		var rd = Math.random();
		rd = 'fn' + parseInt(rd*10000);
		_nowId = rd;
	  $http[method](url, params).success(function(result) {
        idle();
        if(_sucHeap[rd]){
        	_sucHeap[rd](result);
        }
        // ErrorHandle.handle(result)
      }).error(function(reason, status) {
        idle();
        if(_errHeap[rd]){
        	_errHeap[rd](reason,status);
        }
        if (status) {
          if (status === 401) {
          	var target = url.split("?");
        	target = target[0].split("/");
        	target = target[target.length-1];
        	if(target != 'listUnread'||target!='getCurrentUser'){
        		window.location.href = '/login.html';
        	}
          } 
          else {
            // AlertService.alert({
            //   title: "服务端异常",
            //   content: '系统出了点小问题，请稍后重试！'
            //     //content: errorContent
            // });
            SpSlider.slideIn({
              title: "服务端异常",
              content: '系统出了点小问题，请稍后重试！',
              type:'alert'
            });
          }

        }
      });
	};
	var SpHttp = function(opt){
		busy();
		var rd = Math.random();
		rd = 'fn' + parseInt(rd*10000);
		_nowId = rd;
		$http(opt).success(function(res){
			idle();
        	if(_sucHeap[rd]){
        		_sucHeap[rd](res);
        	}
		}).error(function(reason,status){

			idle();

	        if(_errHeap[rd]){
	        	_errHeap[rd](reason,status);
	        }
	        if (status) {
	          if (status === 401) {
	          	var target = url.split("?");
	        	target = target[0].split("/");
	        	target = target[target.length-1];
	        	if(target != 'listUnread'||target!='getCurrentUser'){
	        		window.location.href = '/login.html';
	        	}
	          } 
	          else {
	            SpSlider.slideIn({
	              title: "服务端异常",
	              content: '系统出了点小问题，请稍后重试！',
	              type:'alert'
	                //content: errorContent
	            });
	          }

	        }

		});
		return SpHttp;
	};
	var _get = function(url,opt){
		sendReq(url,opt,'get');
		return SpHttp;
	};
	var _post = function(url,opt){
		sendReq(url,opt,'post');
		return SpHttp;
	};
	var _success = function(fn){
		if(fn){
			_sucHeap[_nowId] = fn;
		}
		return SpHttp;
	};
	var _error = function(fn){
		if(fn){
			_errHeap[_nowId] = fn;
		}
		return SpHttp;
	};
	SpHttp.get = _get;
	SpHttp.post = _post;
	SpHttp.success = _success;
	SpHttp.error = _error;

	return SpHttp;
	
}])

.service('SpSlider',['$compile','$rootScope','$timeout',function($compile,$rootScope,$timeout){
	var tpl = document.querySelector('#sliderTpl').innerHTML;
	var notis = {};
	angular.element('body').append('<div class="sp-sliders-wrap"></div>');
	var slider = angular.element('.sp-sliders-wrap');



	//info:{title:'title',isShow:false,isHide:false,type:'noti/alert'}
	var noti = function(info){ 


		var serial = parseInt(Math.random()*10000);
		serial = 'noti' + serial;

		var scope = $rootScope.$new();
		scope.serial = serial;
		notis[serial] = info;
		for(var i in info){
			scope[i] = info[i];
		}
		scope.time = new Date().getTime();
		var html  = $compile(tpl)(scope);
		slider.append(html);
		$timeout(function(){
			scope.isShow = true;
		},0);
		$timeout(function(){
			if(notis[serial]){
				$rootScope.$broadcast('noti.add',notis[serial]);
				scope.confirm(serial);
			}
		},10000);

		scope.confirm = function(serial){
			notis[serial] = null;
			scope.isShow  = false;
			$timeout(function(){
				scope.isHide = true;
			},300);
			$timeout(function(){
				angular.element('#'+serial).remove();
				scope.$destroy();
			},600);
		};
	};
	return {slideIn : noti};
}])

;













